/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Trucking.java
 *
 * Created on Oct 27, 2010, 4:59:16 PM
 */

package lmclogistics;

import components.Text;
import components.TextArea;
import components.Image;
import components.NButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import sql.Sql;

/**
 *
 * @author Sonia Tan
 */
public class EditTrucking extends javax.swing.JFrame {

    /** Creates new form Trucking */

    Text codeFld = new Text();
    TextArea trucking = new TextArea();

    private Image bg;
    private int id;

    public EditTrucking(int sID) {
        id = sID;
        
        setTitle("Edit Trucking");
        setAlwaysOnTop(true);
        setSize(300, 200);
        centerScreen();
        setVisible(true);
        setBg();
        okCancelButtons();
        initText();
        initFlds();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(null);

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public void setBg(){
        bg = new Image("panel.png",507,460);
        add(bg,-1);

    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void okCancelButtons()
    {
                NButton cancel = new NButton("Cancel",200,130);
                NButton ok = new NButton("Ok",115 ,130);
                add(ok,0);
                add(cancel,0);
                cancel.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    dispose();
                    }
                });

                ok.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    acceptEdit();
                    dispose();
                    //FormWarning warn = new FormWarning("incorrect");
                    }
                });
    }
    public void initText(){
        Text code = new Text();
        Text trucking = new Text();
   
        code.inText("Code:", 15, 15);
        trucking.inText("Trucking",15,40);

        add(code,0);
        add(trucking,0);
  

    }

    private void initFlds() {           
        String query = "SELECT * FROM trucking WHERE trkCode = "+id;
        String qTrkCode;
        String qTrkName;
        
        trucking.initText(60, 25, 100, 38);


        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if(rs.next())
            {
                qTrkCode = rs.getString("trkCode");
                qTrkName = rs.getString("trkName");

                codeFld.inText(qTrkCode, 100, 15);
                trucking.setText(qTrkName);
            }

            Sql.closeSqlConnection(connection);
        } catch(Exception ex){
            Logger.getLogger(EditChassis.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        add(codeFld,0);
        add(trucking,0);

    }

    private void acceptEdit()
    {
        String query = "UPDATE trucking SET trkName='"+trucking.getText()+"' WHERE trkCode="+id;
        
        try{
            Connection connection = Sql.getSqlConnection();
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.executeUpdate();

            Sql.closeSqlConnection(connection);
        } catch(Exception ex)
        {
            Logger.getLogger(EditConsignee.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void centerScreen(){

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the window
    setLocation(x, y);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
